package GUI.Productos;

import java.rmi.RemoteException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;
import service.sale.interfaces.SaleProxyMgt;
import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import article.entities.ArticleVO;

public class MainProductos extends Shell {
	static MainProductos shell;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			 shell = new MainProductos(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the shell.
	 * @param display
	 */
	
	public MainProductos(){
		try {
			Display display = Display.getDefault();
			MainProductos shell = new MainProductos(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}
	public MainProductos(Display display) throws RemoteException, ProxyException {
		
		super(display, SWT.SHELL_TRIM);
		setLayout(new FormLayout());
		
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayout(new FormLayout());
		FormData fd_composite = new FormData();
		fd_composite.bottom = new FormAttachment(0, 278);
		fd_composite.right = new FormAttachment(0, 450);
		fd_composite.top = new FormAttachment(0);
		fd_composite.left = new FormAttachment(0);
		composite.setLayoutData(fd_composite);
		
		Label lblElijaLaOpcion = new Label(composite, SWT.NONE);
		lblElijaLaOpcion.setFont(SWTResourceManager.getFont("Lucida Grande", 13, SWT.NORMAL));
		lblElijaLaOpcion.setAlignment(SWT.CENTER);
		FormData fd_lblElijaLaOpcion = new FormData();
		fd_lblElijaLaOpcion.top = new FormAttachment(0, 17);
		fd_lblElijaLaOpcion.left = new FormAttachment(0, 137);
		lblElijaLaOpcion.setLayoutData(fd_lblElijaLaOpcion);
		lblElijaLaOpcion.setText("ELIJA LA OPCION DESEADA");
		
		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			AddProductos a = new AddProductos(shell, shell.getStyle(), null,null);
			shell.setVisible(false);
			try {
				a.open();
			} catch (RemoteException e1) {
				ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
				ventana.open();
				shell.dispose();
				e1.printStackTrace();
			} catch (ProxyException e1) {
				ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
				ventana.open();
				shell.dispose();
				e1.printStackTrace();
			}
			}
			
		});
		FormData agregar = new FormData();
		agregar.top = new FormAttachment(lblElijaLaOpcion, 17);
		agregar.left = new FormAttachment(0, 129);
		agregar.right = new FormAttachment(100, -130);
		btnNewButton.setLayoutData(agregar);
		btnNewButton.setText("Agregar Nuevo Producto");
		
		Button ver = new Button(composite, SWT.NONE);
		ver.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				/*Ingredient a = new Ingredient ("Ketchup");
				Ingredient [] in = new Ingredient[1];
				in[0]=a;
				CategoryVO cat = new CategoryVO("Hola");
				ArticleVO art = new ArticleVO("Hamburguesa", "Hola",3, in, cat);
				ArticleVO[] arts = new ArticleVO[1];*/
				ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
				ArticleVO[] arts;
				try {
					arts = uso.getArticles();
					verProd ver = new verProd(arts, false);
				//	shell.setVisible(false);
					//ver.open(arts,true);
					
					
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
			
				
				
				
				//ver.
			}
		});
		FormData fd_ver = new FormData();
		fd_ver.top = new FormAttachment(btnNewButton, 17);
		fd_ver.left = new FormAttachment(0, 129);
		fd_ver.right = new FormAttachment(100, -130);
		ver.setLayoutData(fd_ver);
		ver.setText("Ver Productos del Menu");
		
		Button modificar = new Button(composite, SWT.NONE);
		modificar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				/*Ingredient a = new Ingredient ("Ketchup");
				Ingredient [] in = new Ingredient[1];
				in[0]=a;
				CategoryVO cat = new CategoryVO("Hola");
				ArticleVO art = new ArticleVO("Hamburguesa", "Hola",3, in, cat);
				ArticleVO[] arts = new ArticleVO[1];
				arts[0]=art;
				
				*/
				ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
				ArticleVO[] arts;
				try {
					arts = uso.getArticles();
					verProd ver = new verProd(arts, true);
					
					
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
				
			}
		});
		SaleProxyMgt art = ServiceFacade.getInstance().getSaleProxyMgt();
		FormData fd_modificar = new FormData();
		fd_modificar.top = new FormAttachment(ver, 17);
		fd_modificar.left = new FormAttachment(0, 129);
		modificar.setLayoutData(fd_modificar);
		modificar.setText("Modificar/Eliminar Productos ");
		
		Button btnAgregarNuevoCombo = new Button(composite, SWT.NONE);
		btnAgregarNuevoCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				NuevoCombo nuevo = new NuevoCombo (shell, shell.getStyle());
				nuevo.open();
			}
		});
		FormData fd_btnAgregarNuevoCombo = new FormData();
		fd_btnAgregarNuevoCombo.top = new FormAttachment(modificar, 17);
		fd_btnAgregarNuevoCombo.left = new FormAttachment(0, 129);
		fd_btnAgregarNuevoCombo.right = new FormAttachment(100, -130);
		btnAgregarNuevoCombo.setLayoutData(fd_btnAgregarNuevoCombo);
		btnAgregarNuevoCombo.setText("Agregar Nuevo Combo");
		
		Button btnBorrarComboDel = new Button(composite, SWT.NONE);
		btnBorrarComboDel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				BajaCombo b = new BajaCombo (shell, shell.getStyle());
				b.open();
			}
		});
		FormData fd_btnBorrarComboDel = new FormData();
		fd_btnBorrarComboDel.top = new FormAttachment(btnAgregarNuevoCombo, 17);
		fd_btnBorrarComboDel.left = new FormAttachment(0, 137);
		fd_btnBorrarComboDel.right = new FormAttachment(100, -137);
		btnBorrarComboDel.setLayoutData(fd_btnBorrarComboDel);
		btnBorrarComboDel.setText("Borrar Combo del Menu");
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("MENU PRODUCTOS");
		setSize(473, 326);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
